package com.chen.androidstudy.utils

import android.annotation.SuppressLint
import android.text.format.DateUtils
import java.text.SimpleDateFormat
import java.util.*

object TimeUtil {
    private const val TAG = "TimeUtil"

    /**
     * 获取当前时间戳 秒
     */
    fun genTimeStampSeconds(): Long {
        return System.currentTimeMillis() / 1000
    }

    /**
     * 获取当天时间戳
     */
    fun genTimeStampToday(): Long {
        val date = Date()
        val simpleDateFormat = SimpleDateFormat(YYYY_MM_dd)
        val today = simpleDateFormat.format(date.time)
        return simpleDateFormat.parse(today).time
    }

    const val minuteTimeFormat = "mm:ss"
    const val hourTimeFormat = "HH:mm"
    const val monthTimeFormat = "MM/dd HH:mm"
    const val yearTimeFormat = "yyyy/MM/dd HH:mm"
    const val yearMonthDayFormat = "yyyy/MM/dd"
    const val secondFormat = "%02d:%02d:%02d"
    const val secondFormatZh = "%02d时%02d分%02d秒"
    const val YYYY_MM_dd = "yyyy-MM-dd"

    fun getTime(time: Long, pattern: String?): String {
        val date = Date(time)
        return dateFormat(date, pattern)
    }

    fun dateFormat(date: Date?, pattern: String?): String {
        val format = SimpleDateFormat(pattern)
        return format.format(date)
    }


    @SuppressLint("DefaultLocale")
    fun secondFormat(second: Int, format: String?): String? {
        var second = second
        var format = format
        val hour = second / 3600 // 得到时
        second %= 3600 //剩余的秒数
        val minute = second / 60 //得到分
        second %= 60 //剩余的秒
        if (null == format) format = secondFormat
        if (hour > 0) return String.format(format, hour, minute, second)
        if (minute > 0) return String.format(
            format.substring(5),
            minute, second
        )
        return if (second > 0) String.format(format.substring(10), second) else ""
    }

    private fun repair0(v: Int): String? {
        return if (v < 10) "0$v" else v.toString() + ""
    }

    fun genCurrentTimeStampToday(): Long {
        val date = Date()
        return date.time
    }

    fun genTimeStampToday(hour: Int, min: Int): Long {
        val nowDate = Date()
        nowDate.year
        val date = Date(nowDate.year, nowDate.month, nowDate.date, hour, min, 2)
        return date.time
    }
}